package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.mapper.AdminMapper;
import com.ruan.mapper.DepartmentMapper;
import com.ruan.mapper.EmployeeMapper;
import com.ruan.mapper.SalarySetMapper;
import com.ruan.pojo.*;
import com.ruan.pojoExt.SalarySetExt;
import com.ruan.service.PositionService;
import com.ruan.service.SalarySetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
@Slf4j
public class SalarySetServiceImpl extends ServiceImpl<SalarySetMapper, SalarySet> implements SalarySetService {

    @Autowired
    private SalarySetMapper salarySetMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    PositionService positionService;


    /**
     * 员工调薪
     * @param id 员工薪资设置id
     * @param adminId 管理员id
     * @param salarySet 薪资设置
     */
    @Override
    public void setSalary(Integer id ,Integer adminId, SalarySet salarySet) {
        Admin admin = adminMapper.selectById(adminId);
        SalarySet salarySetSelect = salarySetMapper.selectById(id);
        salarySetSelect.setBasicSalary(salarySet.getBasicSalary());
        salarySetSelect.setTrafficSubsidy(salarySet.getTrafficSubsidy());
        salarySetSelect.setMealsSubsidy(salarySet.getMealsSubsidy());
        salarySetSelect.setApprover(admin.getUsername());
        salarySetMapper.updateById(salarySetSelect);
    }


    //同意员工入职审批时为员工设置薪资
    @Override
    public void addSalarySet(Integer adminId,
                             Integer userId,
                             SalarySet salarySet) {
        Employee employee = employeeMapper.selectById(userId);
        Admin admin = adminMapper.selectById(adminId);
        salarySet.setApprover(admin.getUsername());
        salarySet.setDepartmentId(employee.getDepartmentId());
        salarySet.setEmployeeId(employee.getId());
        salarySetMapper.insert(salarySet);
    }

    @Override
    public SalarySetExt getSelfSalarySet(String phone) {
        LambdaQueryWrapper<SalarySet> queryWrapper = new LambdaQueryWrapper<>();
        //-----------------------------------
        LambdaQueryWrapper<Employee> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Employee::getPhone,phone);
        Employee employee = employeeMapper.selectOne(queryWrapper1);
        queryWrapper.eq(SalarySet::getEmployeeId,employee.getId());
        SalarySet salarySet = salarySetMapper.selectOne(queryWrapper);
        SalarySetExt salarySetExt = new SalarySetExt();

        BeanUtils.copyProperties(salarySet,salarySetExt);
        System.out.println("属性迁移完了吗："+salarySetExt);

        LambdaQueryWrapper<Department> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(Department::getId,employee.getDepartmentId());
        Department department = departmentMapper.selectOne(queryWrapper2);

        salarySetExt.setEmployeeName(employee.getName());
        salarySetExt.setDepartmentName(department.getName());

        Position position = positionService.getById(employee.getPositionId());
        if(position != null) {
            salarySetExt.setPosition(position.getPname());
        }
        return salarySetExt;
    }
}
